package Control;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.*;
import java.util.ArrayList;
import java.util.Date;
import Database.*;
import Model.*;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

public class EditSupplier extends JFrame implements ActionListener
{
        private JLabel id, suppliers, jlSuppID, jlName;
        private JTextField jtID, jtName;
        private JButton search, clear, save, cancel;
        private JPanel leftPanel, rightPanel;
        private TableOperations to;
        private JTable jt;
        private DefaultTableModel jtM;
        private ArrayList users;
        private ArrayList<Supplier> test = new ArrayList<Supplier>();
        private Supplier toBeEdited = null;
        private ImageIcon icon = new ImageIcon("frameIcon.png");

        public EditSupplier(TableOperations to)
        {
                GridBagLayout layout = new GridBagLayout();
                GridBagConstraints gc = new GridBagConstraints();

                setIconImage(icon.getImage());
                setTitle("MDL - Edit Supplier");
                setSize(750, 300);
                setLocationRelativeTo(null);
                setResizable(false);
                setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                this.setLayout(new GridLayout(1, 2));

                this.to = to;
                Insets insets = new Insets(15, 15, 13, 3);

                leftPanel = new JPanel();
                leftPanel.setLayout(layout);

                id = new JLabel("ID or Name");
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 0;
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(id, gc);

                jtID = new JTextField(10);
                gc.gridx = 0;
                gc.gridy = 0;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 100, 13, 15);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(jtID, gc);
                
                search = new JButton("Search");
                search.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 150, 0, 125);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(search, gc);
                
                clear = new JButton("Clear");
                clear.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(0, 250, 0, 25);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(clear, gc);
                
                suppliers = new JLabel("Suppliers");
                gc.gridx = 0;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 50, 10, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(suppliers, gc);

                createTable();
                gc.gridx = 0;
                gc.gridy = 3;
                gc.weighty = 1;
                gc.weightx = 3;
                insets = new Insets(10, 25, 20, 30);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                leftPanel.add(new JScrollPane(jt), gc);

                add(leftPanel);

                insets = new Insets(15, 15, 5, 3);

                //rightPanel
                rightPanel = new JPanel();
                rightPanel.setLayout(layout);

                jlName = new JLabel("Name");
                gc.gridx = 0;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 0;
                insets = new Insets(15, 75, 5, 30);
                gc.insets = insets;
                rightPanel.add(jlName, gc);

                jtName = new JTextField();
                gc.gridx = 2;
                gc.gridy = 1;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -125, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(jtName, gc);

                save = new JButton("Save Changes");
                save.addActionListener(this);
                gc.gridx = 0;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, 50, 5, 75);
                gc.insets = insets;
                rightPanel.add(save, gc);

                cancel = new JButton("Cancel");
                cancel.addActionListener(this);
                gc.gridx = 2;
                gc.gridy = 2;
                gc.weighty = 0;
                gc.weightx = 1;
                insets = new Insets(15, -65, 5, 3);
                gc.insets = insets;
                gc.fill = GridBagConstraints.BOTH;
                rightPanel.add(cancel, gc);

                gc.gridx = 3;
                gc.gridy = 2;
                gc.weighty = 1;
                gc.weightx = 1;
                this.add(rightPanel, gc);
        }
        
        public void createTable()
        {
                jtM = new DefaultTableModel();

                jtM.addColumn("Supplier Name");

                fillTable();

                jt = new JTable(jtM);
                jt.addMouseListener(new MouseAdapter()
                {
                        public void mousePressed(MouseEvent e)
                        {
                                int row = jt.getSelectedRow();
                                int supplierID = test.get(row).getSupplierID();
                                String name;
                                if(test.get(row).getName().isEmpty())
                                        name = test.get(row).getName();
                                else
                                        name = test.get(row).getName().trim();
                                toBeEdited = new Supplier(supplierID, name);
                                System.out.println(supplierID);
                                jtName.setText(name);
                        }
                });
        }

        public void clear()
        {
                try
                {
                        jtM.removeRow(0);
                        for (int i = 0; i < jtM.getRowCount(); i++)
                        {
                                jtM.removeRow(i);
                        }

                        test.remove(0);
                        for (int i = 0; i < test.size(); i++)
                        {
                                test.remove(i);
                        }
                } catch (Exception e)
                {
                        System.out.println("No rows to be deleted");
                }
                finally
                {
                        //jtID.setText("");
                        jtName.setText("");
                }
        }

        public void fillTable()
        {               
                if (jtM.getRowCount() >= 1)
                {
                        clear();

                        String[] name = { test.get(test.size() - 1).getName() };
                        jtM.addRow(name);

                } else
                {
                        for (int i = 0; i < test.size(); i++)
                        {
                                String[] name = { test.get(i).getName() };
                                jtM.addRow(name);
                        }
                }
        }

        public void search()
        {
                try
                {
                        test.addAll(to.searchSupplier(Integer.parseInt(jtID.getText())));
                }
                catch(NumberFormatException nfe)
                {
                        test.addAll(to.searchSupplier(jtID.getText()));
                }
                fillTable();
                if(jtM.getRowCount() <= 0)
                        JOptionPane.showMessageDialog(null, "Supplier not found. Hint: Use a CAPITAL LETTER for the 1st Letter");
        }
        
        public void actionPerformed(ActionEvent e)
        {
                if (e.getSource() == cancel)
                        this.dispose();
                else if (e.getSource() == clear)
                        clear();
                else if (e.getSource() == search)
                {
                        if (jtM.getRowCount() > 0)
                        {
                                clear();
                                search();
                        } else
                                search();
                } 
                else if (e.getSource() == save)
                {
                        if(jtName.getText().equals(""))
                                JOptionPane.showMessageDialog(null, "Check Blank Fields");
                        else
                        {
                                String name = jtName.getText();
                                
                                toBeEdited.setName(name);
                                
                                if(to.updateSupplier(toBeEdited))
                                {
                                        JOptionPane.showMessageDialog(null, "Supplier Updated");
                                        clear();
                                }
                                else
                                        JOptionPane.showMessageDialog(null, "Update Unsuccessful");
                        }
                }
        }
}